package com.foreknow.demo8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListPerson {
    public static void main(String[] args) {
        //创建一个集合并向集合中添加3个Person对象，然后遍历集合取出所有Person的信息
        List<Person> list = new ArrayList<>();

        Person p1 = new Person(1,"tom",20);
        Person p2= new Person(2,"fender",21);
        Person p3 = new Person(3,"gibson",22);

        list.add(p1);
        list.add(p2);
        list.add(p3);

        System.out.println("----------------for------------------");
        for(int i = 0;i<list.size();i++){
            Person p = list.get(i);
            System.out.println(p.getName());
        }

        System.out.println("-----------------foreach----------------------");
        for (Person person:list) {
            System.out.println(person.getName());
        }

        System.out.println("----------------------迭代器-------------------");
        Iterator<Person> it = list.iterator();
        while (it.hasNext()) {
            Person person = it.next();
            System.out.println(person.getName());
        }
    }
}
